package cn.picclife.mwx.manager.sys.controller;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.jsoup.helper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.text.ParseException;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import cn.picclife.mwx.common.core.util.crypto.PasswordHashUtil;
import cn.picclife.mwx.manager.base.BaseResult1;
import cn.picclife.mwx.manager.sys.model.bo.UserSessionBo;
import cn.picclife.mwx.manager.sys.model.po.Friend;
import cn.picclife.mwx.manager.sys.model.po.Operator;
import cn.picclife.mwx.manager.sys.service.FriendService;
import cn.picclife.mwx.manager.util.EmailUtil;
import cn.picclife.mwx.manager.util.RestUtils;
import cn.picclife.mwx.manager.util.uniqueseq.UniqueSeq;

/**
 * 伙伴管理控制层类
 * @ClassName FriendController.java
 * @Description 
 * @author Guan JunHui
 * @Date 2017年9月20日
 */
@Controller
@RequestMapping(value = "/api")
public class FriendController {
	
	@Autowired
	private FriendService service;
	
	@ResponseBody
	@RequestMapping(value = "/friend/add_friend")
	public BaseResult1 insertFriend(Friend friend,Operator operator,HttpServletResponse response,HttpServletRequest request){
		BaseResult1 result = new BaseResult1();
		if (StringUtils.isBlank(operator.getOptName())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("公司名称不能为空");
			RestUtils.error(response, result);
			return result;
		}
		
		if (StringUtils.isBlank(operator.getOptNo())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("登录账号不能为空");
			RestUtils.error(response, result);
			return result;
		}
		if (null == operator.getOptPassword() || StringUtils.isBlank(String.valueOf(operator.getOptPassword()))) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("用户密码不能为空");
			RestUtils.error(response, result);
			return result;
		}
		if (null == operator.getOptPasswordT() || StringUtils.isBlank(String.valueOf(operator.getOptPasswordT()))) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("确认密码不能为空");
			RestUtils.error(response, result);
			return result;
		}
		if (!Arrays.equals(operator.getOptPassword(), operator.getOptPasswordT())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("两次密码不一致");
			RestUtils.error(response, result);
			return result;
		}
		if (StringUtils.isBlank(friend.getCompanyType())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("用户类型不能为空");
			RestUtils.error(response, result);
			return result;
		}

		if (null == friend.getApplicationDate()) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("申请日期不能为空");
			RestUtils.error(response, result);
			return result;
		}
		if (StringUtil.isBlank(friend.getPhone())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("联系方式不能为空");
			RestUtils.error(response, result);
			return result;
		}
		if (String.valueOf(friend.getPhone()).length() != 11) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("联系方式长度错误");
			RestUtils.error(response, result);
			return result;
		}
		if (StringUtils.isBlank(friend.getEmail())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("电子邮箱不能为空");
			RestUtils.error(response, result);
			return result;
		}
		if (!EmailUtil.checkEmail(friend.getEmail())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("电子邮箱格式错误");
			RestUtils.error(response, result);
			return result;
		}
		
		UserSessionBo user = (UserSessionBo) request.getSession().getAttribute("user");
		friend.setCreateUser(user.getOptName());
		
		Long serno = UniqueSeq.getUniqueSeq("opt_id", "t_operator");
		operator.setOptId(serno.toString());
		friend.setUserId(operator.getOptNo());
		Date date = new Date();
		operator.setStartDate(date);
		try {
			operator.setEndDate(DateUtils.parseDateStrictly("9999-09-09", "yyyy-MM-dd"));
		} catch (ParseException e) {
//			e.printStackTrace();
		}
		char[] pwd=PasswordHashUtil.createHash(operator.getOptPassword()).toCharArray();//将密码加密
		operator.setOptPassword(pwd);
		operator.setCrtDate(date);
		operator.setMdfDate(date);
		operator.setDataFlag("1");
		if ("".equals(operator.getOptStatus())) {
			operator.setOptStatus("1");
		}
		operator.setCrtUser(user.getOptNo());
		operator.setMdfUser(user.getOptNo());
		operator.setDeptNo("10");
		int num = service.insertFriend(friend, operator);
        Arrays.fill(pwd, ' ');
		if (num == 2) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("账号已经存在");
			return result;
		}else if(num == 0){
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("添加失败");
			return result;
		}else{
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("添加成功");
			return result;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/friend/delete_friends")
	public BaseResult1 deleteFriend(String ids,HttpServletResponse response){
		String[]tempid = ids.split(",");
		int []id = new int[tempid.length];
		for (int i = 0; i < id.length; i++) {
			id[i] = Integer.valueOf(tempid[i]);
		}
		BaseResult1 result = new BaseResult1();
		int flag = service.deleteFriend(id);
		if (flag == 1) {
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("删除成功");
			return result;
		}else{
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("删除失败");
			return result;
		}
	}
	
	@ResponseBody
	@RequestMapping(value = "/friend/query_friends")
	public Map<String, Object> queryFriend(int id){
		return service.queryFriendById(id);
	}
	
	@ResponseBody
	@RequestMapping(value = "/friend/update_friends")
	public BaseResult1 updateFriend(Friend friend,Operator operator,HttpServletResponse response,HttpServletRequest request){
		BaseResult1 result = new BaseResult1();
		if (StringUtils.isBlank(operator.getOptName())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("公司名称不能为空");
			return result;
		}
		if (StringUtils.isBlank(operator.getOptNo())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("登录账号不能为空");
			return result;
		}
		if (null == operator.getOptPassword() || StringUtils.isBlank(String.valueOf(operator.getOptPassword()))) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("用户密码不能为空");
			return result;
		}
		if (null == operator.getOptPasswordT() || StringUtils.isBlank(String.valueOf(operator.getOptPasswordT()))) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("确认密码不能为空");
			return result;
		}
		if (!Arrays.equals(operator.getOptPassword(), operator.getOptPasswordT())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("两次密码不一致");
			return result;
		}
		if (StringUtils.isBlank(friend.getCompanyType())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("用户类型不能为空");
			return result;
		}

		if (null == friend.getApplicationDate()) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("申请日期不能为空");
			return result;
		}
		if (StringUtils.isBlank(friend.getPhone())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("联系方式不能为空");
			return result;
		}
		if (String.valueOf(friend.getPhone()).length() != 11) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("联系方式长度错误");
			return result;
		}
		if (StringUtils.isBlank(friend.getEmail())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("电子邮箱不能为空");
			return result;
		}
		if (!EmailUtil.checkEmail(friend.getEmail())) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("电子邮箱格式错误");
			return result;
		}
		
		UserSessionBo userSessionBo = (UserSessionBo) request.getSession().getAttribute("user");
		String optNo=userSessionBo.getOptNo();
		friend.setEditUser(optNo);
		operator.setMdfDate(new Date());
		operator.setMdfUser(optNo);
		int num = service.updateFriend(friend, operator);
		if (num==0) {
			result.setRet(BaseResult1.BIZ_ERROR);
			result.setMsg("修改失败");
			return result;
		}else{
			result.setRet(BaseResult1.SUCCESS);
			result.setMsg("修改成功");
			return result;
		}
	}
	
	@ResponseBody
	@RequestMapping("/friend/page_friend")
	public Map<String, Object>pageFriend(Friend friend,Operator operator){
		Map<String, Object>pageMap = new HashMap<String,Object>();
		pageMap.put("friendList", service.pageFriend(friend, operator));
		pageMap.put("total", friend.getTotal());
		pageMap.put("pageCount", friend.getPageCount());
		pageMap.put("pageSize", friend.getPageSize());
		pageMap.put("pageNumber", friend.getPageNumber());
		return pageMap;
	}
	
}
